﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using System.Runtime.InteropServices;
using PDAMain.Entity;

namespace PDAMain.Forms.CustomerWork
{
    public partial class CustomerToFastRepair : BaseForm
    {
        public string customerID;
        private bool scanFlag = true;
        public CustomerToFastRepair()
        {
            InitializeComponent();
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            if (scanFlag)
            {
                MessageBox.Show("请扫描对应资产编号");
                return;
            }
            if (txtProcessContent.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请填写备注");
                return;
            }
            if (MessageBox.Show("你确定要执行此操作吗？", "提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
                return;
            //确定
            string strActiveID;
            //string strAddress;
            //string strContactName;
            //string strContactPhone;
            string strDescription;
            string strDutyPerson;
           // string strRemark;
            string strAssetNo;
            string strProcessReason;
            string strEstdur;

            strActiveID = lblActiveID.Text.ToString();
            //strAddress = lblAddress.Text.ToString();
            //strContactName = lblContactName.Text.ToString();
            //strContactPhone = lblContactPhone.Text.ToString();
            strDescription = lblDescription.Text.ToString();
            strDutyPerson = lblDutyPerson.Text.ToString();
            //strRemark = lblRemark.Text.ToString();
            strAssetNo = lblAssetNo.Text.ToString();
            strProcessReason = txtProcessContent.Text.ToString();
            strEstdur = txtEstdur.Text.ToString();

            //保存數據
            if (DataHelper.CustomerWorkDao.FastWorkOrder(customerID, "9", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), strProcessReason, "", strAssetNo, strEstdur))
            {
                MessageBox.Show("成功同步数据至远程数据库");
            }
            else
            {
                bool result1 = DataHelper.ApplyToRepairDao.insertLacolTable(strAssetNo, Identity.dutyPerson, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), strProcessReason, "", "0", "", strEstdur);
                bool result2 = DataHelper.CustomerWorkDao.insertLacolTable(customerID, "9", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), strProcessReason);
                if (result1 && result2)
                {
                    MessageBox.Show("同步数据失败，保存至本地数据库");
                }
                else
                {
                    MessageBox.Show("保存失败");
                }
            }
            backToList();
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            //返回
            CustomerDetail detail = new CustomerDetail();
            detail.customerID = customerID;
            detail.Show();
            this.Close();
        }

        private void CustomerToFastRepair_Load(object sender, EventArgs e)
        {
            //加载扫描设备
            LoadBarCode();
            BindData();
        }

        //加载数据
        private void BindData()
        {
            //獲取數據
            DataTable dt = new DataTable();
            dt = DataHelper.CustomerWorkDao.getDetail(customerID).Tables[0];
            if (dt.Rows.Count == 0)
                return;

            lblActiveID.Text = dt.Rows[0]["CWOrder_No"].ToString();
            //lblAddress.Text = dt.Rows[0]["Address"].ToString();
           // lblContactName.Text = dt.Rows[0]["User_Name"].ToString();
           // lblContactPhone.Text = dt.Rows[0]["User_Phone"].ToString();
            lblDescription.Text = dt.Rows[0]["DESCRIPTION"].ToString();
            lblDutyPerson.Text = Identity.dutyPerson; //dt.Rows[0]["dutyPerson"].ToString();
           // lblRemark.Text = dt.Rows[0]["Comment"].ToString();
            
        }

        public override void OnBarCodeNotify(byte[] BarCodeData, int nLength)
        {
            EnableBarCode(false);

            string BarCode = Encoding.Default.GetString(BarCodeData, 0, nLength);
            lblAssetNo.Text = BarCode;

            //scanFlag置为false后才能保存本窗体数据
            scanFlag = false;

            //string ss = BarCode;
            EnableBarCode(true);
        }

        private void LoadBarCode()
        {
            // 打开BarCode扫描设备
            if (!OpenBarCode())
            {
                int ErrCode = Marshal.GetLastWin32Error();
                MessageBox.Show("Fail to Open BarCode :(, ErrCode = " + ErrCode.ToString(), "Error");
            }

            EnableBeeper(true);
            EnableVibrater(true);
        }

        private void CustomerToFastRepair_Closed(object sender, EventArgs e)
        {
            //关闭扫描设备
            CloseBarCode();
        }

        private void backToList()
        {
            CustomerList list = new CustomerList();
            list.Show();
            this.Close();
        }
    }
}